﻿Option Explicit On
Option Strict On

Imports System.Text
Imports System.Text.RegularExpressions

''' <summary>
''' Modulo Validaciones: Para comprobar los campos introducidos
''' </summary>
''' <author>Jose Manuel Rizo</author>

Module Validaciones



    'Validamos los campos de los formularios
    Public Function comprobarEmail(ByVal texto As String) As Boolean

        ' retorna true o false   
        Return Regex.IsMatch(texto, _
                "^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$")

    End Function

    Public Function CalculaNIF(ByVal strA As String) As String
        '----------------------------------------------------------------------
        ' Calcular la letra del NIF
        ' Código original adaptado a Visual Basic                   (13/Sep/95)
        ' Adaptado a Visual Basic .NET (VB 9.0/2008)                (09/May/08)
        ' y convertido en función que devuelve el NIF correcto
        '----------------------------------------------------------------------
        Const cCADENA As String = "TRWAGMYFPDXBNJZSQVHLCKE"
        Const cNUMEROS As String = "0123456789"
        Dim a, b, c, NIF As Integer
        Dim sb As New StringBuilder

        strA = Trim(strA)
        If Len(strA) = 0 Then Return ""

        ' Dejar sólo los números
        For i As Integer = 0 To strA.Length - 1
            If cNUMEROS.IndexOf(strA(i)) > -1 Then
                sb.Append(strA(i))
            End If
        Next

        strA = sb.ToString
        a = 0
        NIF = CInt(Val(strA))
        Do
            b = CInt(Int(NIF / 24))
            c = NIF - (24 * b)
            a = a + c
            NIF = b
        Loop While b <> 0
        b = CInt(Int(a / 23))
        c = a - (23 * b)

        Return strA & Mid(cCADENA, CInt(c + 1), 1)
    End Function

    Public Function comprobarDNI(ByVal valor As String) As Boolean
        Dim aux As String

        valor = valor.ToUpper ' ponemos la letra en mayúscula
        aux = valor.Substring(0, valor.Length - 1) ' quitamos la letra del NIF

        If aux.Length >= 7 AndAlso IsNumeric(aux) Then
            aux = CalculaNIF(aux) ' calculamos la letra del NIF para comparar con la que tenemos
        Else
            Return False
        End If

        If valor <> aux Then ' comparamos las letras
            Return False
        End If

        Return True
    End Function

    Public Function comprobarTelefono(ByVal texto As String) As Boolean

        'TODO

        If texto(0) = "9" Or texto(0) = "6" Then
            If texto.Length <> 9 Then
                Return False
            End If
        End If

        comprobarTelefono = True
    End Function

    Public Function comprobarVacio(ByVal texto As String) As Boolean
        If texto = "" Then
            comprobarVacio = True
        End If
        comprobarVacio = False
    End Function

    Function CaracteresInvalidos(ByVal p1 As String) As Boolean
        Dim cadena As String = "!$%&/()=?¿*" & Chr(34)
        Dim fallo As Boolean

        For Each elemento As Char In cadena
            If p1.IndexOf(elemento) >= 0 Then
                fallo = True
            End If
        Next

        Return fallo
    End Function

End Module
